/**
 * 用于阻止点击的遮蔽层<br>
 * 一般应用于弹出窗口等操作，实现单模态操作
 */

var mask_layer;

//页面载入初始化
$( "body" ).ready( function( e )
{
	mask_layer = new MaskLayer();
	mask_layer.init();
} );

//窗口大小改变
$( window ).resize( function( e )
{
	mask_layer.resize();
} );

function MaskLayer()
{
	var html = "<div id='__MODEL_WIN_MASK' style='display:block;position:absolute;z-index:0;background-color:#000;filter:alpha(opacity=20);opacity:0.2;'></div>";

	var _useCount = 0;// 使用计数
	var $mask;

	this.init = function()
	{
		$( "body" ).append( html );
		$mask = $( "#__MODEL_WIN_MASK" );
		$mask.hide();
		this.resize();
	};

	this.resize = function()
	{
		var bw = $( "body" ).width();
		var bh = $( "body" ).height();

		$mask.width( bw + 5 );
		$mask.height( bh + 5 );
		$mask.move( 0, 0 );
	};

	this.open = function( index )
	{
		_useCount++;

		this.resize();
		this.index( index );
		$mask.show();
	};

	this.index = function( index )
	{
		if( typeof ( index ) != "undefined" && !isNaN( index ) )
		{
			$mask.css( "z-index", index );
		}
	};

	this.close = function()
	{
		_useCount--;

		if( _useCount <= 0 )
		{
			__close();
		}
	};

	function __close()
	{
		$mask.hide();
	}
}
